package ktarou.m1k4.icu.window;

import java.util.HashMap;
import java.util.Map;

import ktarou.m1k4.icu.handler.UploadHandler;
import ktarou.m1k4.icu.util.Category;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class UploadWindow {
	private Shell shell;
	private Display display;
	private Button uploadButton;
	private ProgressBar bar;
	private Label aLabel;
	private Label bLabel;
	private Label cLabel;
	private Label dLabel;
	private Label eLabel;
	private Combo c;
	public UploadWindow() {
		this.display = Display.getCurrent();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		init();
	}

	private void init() {
		shell.setLayout(new GridLayout());
		 c = new Combo(shell, SWT.READ_ONLY);
		 c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		 c.setItems(Category.getValues());
		Group aGroup = new Group(shell, SWT.NULL);
		aGroup.setText("File 1");
		aGroup.setLayout(new GridLayout(2, false));
		aGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		Group bGroup = new Group(shell, SWT.NULL);
		bGroup.setText("File 2");
		bGroup.setLayout(new GridLayout(2, false));
		bGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		Group cGroup = new Group(shell, SWT.NULL);
		cGroup.setText("File 3");
		cGroup.setLayout(new GridLayout(2, false));
		cGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		Group dGroup = new Group(shell, SWT.NULL);
		dGroup.setText("File 4");
		dGroup.setLayout(new GridLayout(2, false));
		dGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		Group eGroup = new Group(shell, SWT.NULL);
		eGroup.setText("File 5");
		eGroup.setLayout(new GridLayout(2, false));
		eGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		Group controlGroup = new Group(shell, SWT.NULL);
		controlGroup.setText("Control");
		controlGroup.setLayout(new GridLayout(2, false));
		controlGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		bar = new ProgressBar(controlGroup,SWT.NONE);
		bar.setLayoutData(new GridData(GridData.FILL_BOTH));
	    bar.setSize(200, 32);
	    uploadButton = new Button(controlGroup, SWT.PUSH);
	    uploadButton.setText("Upload");
	    uploadButton.setLayoutData(new GridData(GridData.FILL_BOTH));
	    uploadButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				Map<String,String> valueToPost = new HashMap<String,String>();
				if(!aLabel.getText().equals("Drag and drop file loe kesini")){
					String cat_selector = Category.stringToCategory(c.getText()).getCode();
					valueToPost.put("cat_selector",cat_selector );
					valueToPost.put("file", aLabel.getText());
					UploadHandler.getInstance().upload(valueToPost);
				}
			}
		});
		final Composite aBorder = new Composite(aGroup, SWT.BORDER);
		aBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
		aLabel = new Label(aBorder, SWT.LEFT | SWT.WRAP);
		aLabel.setText("Drag and drop file loe kesini");
		aLabel.pack();
		setDropTarget(aBorder);
		final Composite bBorder = new Composite(bGroup, SWT.BORDER);
		bBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
		bLabel = new Label(bBorder, SWT.LEFT | SWT.WRAP);
		bLabel.setText("Drag and drop file loe kesini");
		bLabel.pack();
		setDropTarget(bBorder);
		final Composite cBorder = new Composite(cGroup, SWT.BORDER);
		cBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
		cLabel = new Label(cBorder, SWT.LEFT | SWT.WRAP);
		cLabel.setText("Drag and drop file loe kesini");
		cLabel.pack();
		setDropTarget(cBorder);
		final Composite dBorder = new Composite(dGroup, SWT.BORDER);
		dBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
		dLabel = new Label(dBorder, SWT.LEFT | SWT.WRAP);
		dLabel.setText("Drag and drop file loe kesini");
		dLabel.pack();
		setDropTarget(dBorder);
		final Composite eBorder = new Composite(eGroup, SWT.BORDER);
		eBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
		eLabel = new Label(eBorder, SWT.LEFT | SWT.WRAP);
		eLabel.setText("Drag and drop file loe kesini");
		eLabel.pack();
		setDropTarget(eBorder);

		shell.setSize(600, 600);
		// shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	public void setDropTarget(final Composite label) {
		int operations = DND.DROP_MOVE;
		final DropTarget dropTarget = new DropTarget(label, operations);
		// Data should be transferred in plain text format.
		Transfer[] formats = new Transfer[] { FileTransfer.getInstance() };
		dropTarget.setTransfer(formats);
		dropTarget.addDropListener(new DropTargetListener() {
			
			public void drop(DropTargetEvent event) {
				if (FileTransfer.getInstance().isSupportedType(
						event.currentDataType)) {
					String[] text = (String[]) event.data;
					Control[] comp = label.getChildren();
					aLabel = (Label)comp[0];
					aLabel.setText(text[0]);
					aLabel.pack();
				}
			}

			public void dropAccept(DropTargetEvent event) {
			}

			@Override
			public void dragEnter(DropTargetEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void dragLeave(DropTargetEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void dragOperationChanged(DropTargetEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void dragOver(DropTargetEvent arg0) {
				// TODO Auto-generated method stub
				
			}

		});
	}

}
